package com.zyx.flink.realtime.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.flink.api.java.tuple.Tuple2;

import java.util.List;

/**
 * @author zyx
 * @since 2021/6/11 19:52
 * desc: 维度关联工具
 */
@Slf4j
public class DimUtil {
    public static JSONObject getDimInfoNoCache(String tableName, Tuple2<String, String> tuple) {
        // 拼接查询条件
        String sql = "select * from " + tableName + " where " + tuple.f0 + "='" + tuple.f1 + "'";
        System.out.println("查询维度的SQL:" + sql);
        List<JSONObject> dimList = HikariPhoenixUtil.queryList(sql, JSONObject.class);
        JSONObject dimJsonObj = null;
        //对于维度查询来讲，一般都是根据主键进行查询，不可能返回多条记录，只会有一条
        if (dimList.size() > 0) {
            dimJsonObj = dimList.get(0);
            log.info("++++++++++++++++++++Phoenix查询成功, 数据为 {}", dimJsonObj);
        } else {
            log.error("++++++++++++++++++++维度数据没有找到:" + sql);
        }
        return dimJsonObj;
    }
}
